<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'shop:sales:shopSalesPackage:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
        <a-button type="success" @click="handleFpSalePackect('1')" v-auth="'shop:sales:shopSalesPackage:edit'">
          <Icon icon="simple-line-icons:cursor" /> {{ t('发布套餐包') }}
        </a-button>
        <a-popconfirm placement="bottom" title="是否确认批量取消发布？" ok-text="是" cancel-text="否" @confirm="handleFpSalePackect('0')">
          <a-button type="error" v-auth="'shop:sales:shopSalesPackage:view'">
            <Icon icon="simple-line-icons:close" /> {{ t('批量取消发布') }}
          </a-button>
        </a-popconfirm>
        <a-button type="default" @click="handleImport()" v-auth="'shop:sales:shopSalesPackage:edit'">
          <Icon icon="ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.title }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <xqform @register="registerxqDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsShopSalesShopSalesPackageList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent,ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    shopSalesPackageDelete,
    shopSalesPackageListData,
    shopSalesPackageUpdateFbStatus
  } from '/@/api/shop/sales/shopSalesPackage';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import xqform from './xqform.vue';
  import { useModal } from '/@/components/Modal';
  import FormImport from './formImport.vue';

  const { t } = useI18n('shop.sales.shopSalesPackage');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('套餐包管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('标题'),
        field: 'title',
        component: 'Input',
      },
      {
        label: t('生效日期'),
        field: 'beginDate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD',
          //showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('失效日期'),
        field: 'endDate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD',
          //showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('发布状态'),
        field: 'fbStatus',
        component: 'Select',
        componentProps: {
          dictType: 'shop_fb_status',
          allowClear: true,
        },
      },
      // {
      //   label: t('状态'),
      //   field: 'status',
      //   component: 'Select',
      //   componentProps: {
      //     dictType: 'sys_search_status',
      //     allowClear: true,
      //     onChange: handleSuccess,
      //   },
      // },
      {
        label: t('套餐介绍'),
        field: 'remarks',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('标题'),
      dataIndex: 'title',
      key: 'a.title',
      sorter: true,
      width: 180,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('生效日期'),
      dataIndex: 'beginDate',
      key: 'a.begin_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('失效日期'),
      dataIndex: 'endDate',
      key: 'a.end_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('发布状态'),
      dataIndex: 'fbStatus',
      key: 'a.fb_status',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'shop_fb_status',
      fixed: 'right'
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'sys_search_status',
      ifShow: false,
    },
    {
      title: t('创建时间'),
      dataIndex: 'createDate',
      key: 'a.create_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('套餐介绍'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 230,
      align: 'left',
      ifShow: false,
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑套餐包'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'shop:sales:shopSalesPackage:edit',
        ifShow: () => record.fbStatus !== '1',
      },
      {
        icon: 'ant-design:bars-outlined',
        title: t('套餐包详情'),
        onClick: handlexqForm.bind(this, { id: record.id }),
        auth: 'shop:sales:shopSalesPackage:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除套餐包'),
        popConfirm: {
          title: t('是否确认删除套餐包'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'shop:sales:shopSalesPackage:edit',
        ifShow: () => record.fbStatus !== '1',
      },
    ],
  };


  const [registerxqDrawer, { openDrawer: openxqDrawer }] = useDrawer();
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRows }] = useTable({
    api: shopSalesPackageListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  function handlexqForm(record: Recordable) {
    openxqDrawer(true, record);
  }



  async function handleDelete(record: Recordable) {
    const res = await shopSalesPackageDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  // 发布套餐包
  async function handleFpSalePackect(ope) {
    if (getSelectRows().length) {
      var selIds = ref('');
      getSelectRows().forEach((item) => {
        selIds.value += item.id + ',';
      });
      const res = await shopSalesPackageUpdateFbStatus({ selIds: selIds.value,ope})
      showMessage(res.message);
      handleSuccess();
    } else {
      showMessage('请先选择一行数据！');
    }
  }

  // // 批量取消发布
  // async function handleCancel() { 
  //   if (getSelectRows().length) {
  //       var selIds = ref('');
  //       getSelectRows().forEach((item) => {
  //         selIds.value += item.id + ',';
  //       });
  //       const res = await shopSalesPackageUnPublish({selIds:selIds.value})
  //       showMessage(res.message);
  //       handleSuccess();
  //     } else {
  //       showMessage('请先选择一行数据！');
  //     }
  // }

  function handleSuccess() {
    reload();
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }  
</script>
